﻿using MangoMes.Commont.DTOs.ProductDTOs;
using MangoMes.Commont.DTOs;
using MediatR;
using MangoMes.Commont.Infrastructures.Interfaces;
using MangoMes.Commont.DapperHelps;

namespace ManagoMes.Read.API.Applications.ProductionWorkOrders
{
    public class ProductionOrdersBackfillHander : IRequestHandler<ProductionOrdersBackfillCommand, ResultsDTO<ProductionBackDTO>>
    {
        private readonly IBateRepository<ProductionBackDTO> production;

        public ProductionOrdersBackfillHander(IBateRepository<ProductionBackDTO> production)
        {
            this.production = production;
        }

        public async Task<ResultsDTO<ProductionBackDTO>> Handle(ProductionOrdersBackfillCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ResultsDTO<ProductionBackDTO> res = new ResultsDTO<ProductionBackDTO>();
                DapperHelper dapper = new DapperHelper();
                ProductionBackDTO info=new ProductionBackDTO();
                string sql = @$"SELECT * FROM clientmanagement a RIGHT JOIN productionworkorders b on a.ClientNo=b.CustomerNo 
                               LEFT JOIN productmanage c on b.ProductsNo=c.ProductManageNode where b.ProductionWorkOrdersNo='{request.ProNo}'";
                res.Objects = production.GetAll(sql, info, dapper.MangoMesRead).FirstOrDefault();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
